import zmq
import threading
import time,sys
from random import choice,sample
from api import *
sys.path.append("./")

class Monitor(object):
    """docstring for Monitor"""
    def __init__(self,app_type):
        self.app_type=app_type
        self.context=zmq.Context()
        self.socket=context.socket(zmq.DEALER)
        self.socket.setsockopt(zmq.IDENTITY, self.app_type)
        self.poller = zmq.Poller()
        self.poller.register(socket, zmq.POLLIN)
    
    def reg_guard(self):
        pass

    #def run(self):
    #   pass

    def check_policy(self):
        pass






class ClientTask(threading.Thread):
    """ClientTask"""
    def __init__(self):
        threading.Thread.__init__ (self)
        self.identity='swift_object'
        self.count=0
        #self.maxreq=int(reqn)
        #elf.msgsize=int(msgs)
        #self.msg="1"*self.msgsize

    def run(self):
        context = zmq.Context()
        socket = context.socket(zmq.DEALER)
        identity = self.identity
        socket.setsockopt(zmq.IDENTITY, identity )
        socket.connect('ipc:///tmp/guard_app.ipc')
        print 'client %s started' % (identity)
        poll = zmq.Poller()
        poll.register(socket, zmq.POLLIN)
        reqs = 0
        while 1:
            #for i in xrange(5):
            sockets = dict(poll.poll(0))
            if socket in sockets:
                if sockets[socket] == zmq.POLLIN:
                    msg = socket.recv()
                    print 'client %s received: %s' % (identity, msg)
                    
                
        
            """
            test

            """
            msg={}
            msg["GROUP"]="ANY"
            msg["TARGET"]="ANY"
            msg["EFFECT"]="ANY"
            msg["OWNER"]="ANY"
            msg["TIME"]="ANY"
            msg["ACTION"]="GET"
            members = [attr for attr in dir(Http()) if not callable(attr) and not attr.startswith("__")]
            for m in members:
                msg[m]="".join(sample('zyxwvutsrqponmlkjihgfedcba',10))
            #print msg
            
            msg="DEBUG"
            socket.send(msg)
            time.sleep(10)

        socket.close()
        context.term()


if __name__ == '__main__':
    #client_no=sys.argv[1]
    #req_no=sys.argv[2]
    #msg_size=sys.argv[3]
    st=time.time()
    for i in xrange(int(1)):
        client = ClientTask()
        client.start()
    ed=time.time()
    print ed-st
